package com.bawei.module_motocircle.ui.post.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.bawei.module_motocircle.R
import com.bawei.module_motocircle.databinding.LayoutItemPostListBinding

import com.bawei.module_motocircle.model.PostItem

/**
 * @author yaotianxue
 * @description
 * @date 2025/6/2
 */
class PostListAdapter(
    private val onItemClick: ((PostItem, Int) -> Unit)? = null,
    private val onUserClick: ((PostItem) -> Unit)? = null,
//    private val onImageClickWithThumbnail: ((Int, Int, List<Image>, List<Info>) -> Unit)? = null,
    private val onShareClick: ((PostItem) -> Unit)? = null,
    private val onCommentsClick: ((PostItem) -> Unit)? = null,
    private val onLikeClick: ((PostItem, Int) -> Unit)? = null,
    private val onMoreActionClick: ((PostItem, Int) -> Unit)? = null,
    private val onLikeViewClick: ((PostItem, Int, View) -> Unit)? = null,
    private val onCollectionViewClick: ((PostItem, Int, View) -> Unit)? = null,
    private val onFollowViewClick: ((PostItem, Int, View) -> Unit)? = null,
    // 是否显示关系
    private val showRelationState: Boolean = true,
):RecyclerView.Adapter<PostViewHolder>() {
    private val mData = mutableListOf<PostItem>()

    fun setData(data:List<PostItem>){
        mData.clear()
        mData.addAll(data)
        val size = mData.size
        notifyItemRangeInserted(size-1,data.size)
    }


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
        val binding = LayoutItemPostListBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return PostViewHolder(
            onItemClick = onItemClick,
            onUserClick = onUserClick,
//            onImageClickWithThumbnail = onImageClickWithThumbnail,
            onShareClick = onShareClick,
            onCommentsClick = onCommentsClick,
            onLikeClick = onLikeClick,
            onMoreActionClick = onMoreActionClick,
            onLikeViewClick = onLikeViewClick,
            onCollectionViewClick = onCollectionViewClick,
            onFollowViewClick = onFollowViewClick,
            mBinding = binding)
    }

    override fun getItemCount(): Int {
        return mData.size
    }

    override fun onBindViewHolder(
        holder: PostViewHolder,
        position: Int,
        payloads: MutableList<Any>
    ) {
       if (payloads.isEmpty()){
           super.onBindViewHolder(holder, position, payloads) //注意！！
       }else{
           holder.mBinding.apply {
               if(mData[position].if_like == 1){
                   ivLike.setImageResource(R.mipmap.icon_like_checked)
               }else{
                   ivLike.setImageResource(R.mipmap.icon_like)
               }
               if(mData[position].if_collect == 1){
                   ivCollection.setImageResource(R.mipmap.icon_collection_checked)
               }else{
                   ivCollection.setImageResource(R.mipmap.icon_collection)
               }
               tvLike.text = mData[position].formatLikeNumber()
               tvCollection.text = mData[position].formatCollectionNumber()
               tvShare.text = mData[position].formatShareNumber()
           }
       }
    }

    override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
        mData[position].needShowFollowState = showRelationState
        //databinding数据绑定
        holder.mBinding.holder = holder
        holder.mBinding.post = mData[position]

        //自己处理部分UI绑定
        holder.initView(mData[position],position)

    }
    fun getItemByPosition(position:Int):PostItem?{
        if (position < 0 || position >= mData.size ){
            return null
        }
         return mData[position]
    }

    /**
     * 局部增量刷新
     */
    fun updatePostItem(postItem: PostItem){
        repeat(itemCount){
            val  item = mData[it]
            if (item.posts_id == postItem.posts_id){
                item.apply {
                    if_like = postItem.if_like
                    if_collect = postItem.if_collect
                    like_number = postItem.like_number
                    collect_number = postItem.collect_number
                    share_number = postItem.share_number
                    notifyItemChanged(it,"update")
                    return
                }
            }

        }

    }

    fun getPostItem(position: Int):PostItem{
        return mData[position]
    }
}